package com.trace.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import com.trace.dao.BookDao;
import com.trace.entity.Book;

public class BookDaoImpl implements BookDao {

	public List<Book> findBookByCondition(String conditionString) {
		final String sql = "select * from book where 1=1 " + conditionString;
		Connection conn = null;
		ResultSet rs = null;
		Statement stmt = null;
		Book book = null;
		List<Book> listBook = null;
		try {
			conn = DBUtils.getConnection();
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_READ_ONLY);

			rs = stmt.executeQuery(sql);

			if (rs != null) {
				listBook = new LinkedList<Book>();
			} else {
				return null;
			}
			while (rs.next()) {
				book = new Book();
				for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
					book.setBookId(rs.getInt("book_Id"));
					book.setBookType(rs.getInt("book_Type"));
					book.setBookName(rs.getString("book_Name"));
					book.setBookDesc(rs.getString("book_Desc"));
					book.setBookPublisher(rs.getString("book_Publisher"));
					book.setBookCount(rs.getInt("book_Count"));
					book.setBookPrice(rs.getFloat("book_Price"));

				}
				listBook.add(book);
			}
			System.out.println("=======listBook count:" + listBook.size());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtils.free(rs, stmt, conn);
		}
		return listBook;
	}

	public boolean updateBookCount(Book book) {
		StringBuffer sql = new StringBuffer();
		sql.append("update book set book_count = " + book.getBookCount());
		sql.append(" where book_id = " + book.getBookId());
		Connection conn = null;
		ResultSet rs = null;
		Statement stmt = null;
		try {
			conn = DBUtils.getConnection();
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_READ_ONLY);

			stmt.executeUpdate(sql.toString());

			System.out.println("=======update bbook count");
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			DBUtils.free(rs, stmt, conn);
		}
	}

	public Book findBookByID(Book paramBook) {
		final String sql = "select * from book where book_id = "
				+ paramBook.getBookId();
		Connection conn = null;
		ResultSet rs = null;
		Statement stmt = null;
		Book book = null;
		try {
			conn = DBUtils.getConnection();
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_READ_ONLY);

			rs = stmt.executeQuery(sql);

			if (rs != null) {
				book = new Book();
			} else {
				return null;
			}
			while (rs.next()) {
				for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
					book.setBookId(rs.getInt("book_Id"));
					book.setBookType(rs.getInt("book_Type"));
					book.setBookName(rs.getString("book_Name"));
					book.setBookDesc(rs.getString("book_Desc"));
					book.setBookPublisher(rs.getString("book_Publisher"));
					book.setBookCount(rs.getInt("book_Count"));
					book.setBookPrice(rs.getFloat("book_Price"));

				}
			}
			System.out.println("=======book find:" + book.getBookId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtils.free(rs, stmt, conn);
		}
		return book;
	}

}
